package com.loser.config;

import com.loser.redis.core.RedisHandler;
import com.loser.utils.ContextUtil;
import com.loser.utils.RedisCache;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 * 利用 InstantiationAwareBeanPostProcessor 接口的类会优先于 Bean
 *
 * @author alan
 * @date 2022/6/21 21:41
 */
@Configuration
@EnableConfigurationProperties
public class UtilConfiguration extends InstantiationAwareBeanPostProcessorAdapter implements ApplicationContextAware {


    @Bean
    public RedisCache cache(RedisHandler redisHandler) {
        return new RedisCache(redisHandler);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ContextUtil.setApplicationContext(applicationContext);
    }

}
